package com.zy.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.constant.MessageConstant;
import com.zy.constant.RedisConstant;
import com.zy.entity.PageResult;
import com.zy.entity.QueryPageBean;
import com.zy.entity.Result;
import com.zy.pojo.Setmeal;
import com.zy.service.SetmealService;
import com.zy.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

	@Reference
	private SetmealService setmealService;

	@Autowired
	private JedisPool jedisPool;
	@RequestMapping("/upload")
	public Result upload(@RequestParam("imgFile") MultipartFile file){
		try {
			//给文件创建一个唯一的名字
			String filename = file.getOriginalFilename();
			String newName = UUID.randomUUID().toString() + filename;
			QiniuUtils.upload2Qiniu(file.getBytes(),newName);
			//把上传的文件传到redis的集合中
			jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);

			//还要把图片名字返回去，这样才能在七牛上创建的图片服务器才能找到图片，进行回显和下一步保存
			return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newName);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
		}
	}
	@PreAuthorize("hasAuthority('SETMEAL_ADD')")
	@RequestMapping("/add")
	public Result add(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
		try {
			setmealService.add(checkgroupIds,setmeal);
			return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
		}
	}

	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
		return setmealService.findPage(queryPageBean);
	}

	@RequestMapping("/findCheckitemAndCheckgroupId")
	public Result findCheckitemAndCheckgroupId(Integer id){
		try {
			List<Integer> list=setmealService.findCheckitemAndCheckgroupId(id);
			return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
		}

	}
	@PreAuthorize("hasAuthority('SETMEAL_EDIT')")
	@RequestMapping("/edit")
	public Result edit(Integer[] checkitemIds,@RequestBody Setmeal setmeal){

		try {
			setmealService.edit(checkitemIds,setmeal);
			return new Result(true,"编辑套餐成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"编辑套餐失败");
		}
	}

	@RequestMapping("/img")
	public Result img(Integer id){

		try {
			String img=setmealService.img(id);
			return new Result(true,"回显图片成功",img);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"回显图片失败");
		}

	}
	@PreAuthorize("hasAuthority('SETMEAL_DELETE')")
	@RequestMapping("/delete")
	public Result delete(Integer id){

		try {
			setmealService.delete(id);
			return new Result(true,"删除套餐成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"删除套餐失败");
		}


	}


}
